package com.ruoyi.system.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.House;
import com.ruoyi.system.domain.doc.HouseDoc;
import com.ruoyi.system.domain.dto.HouseChangeStateDto;
import com.ruoyi.system.domain.dto.HouseQueryDto;
import com.ruoyi.system.domain.vo.HouseInfoVo;
import com.ruoyi.system.domain.vo.HouseMapVo;
import com.ruoyi.system.domain.vo.HouseQueryVo;
import com.ruoyi.system.mapper.HouseMapper;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.service.IHouseService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HouseServiceImpl implements IHouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    @Override
    public Map selectHouseList(HouseQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<HouseQueryVo> page = houseMapper.selectHouseListByHouseQueryDto(dto);

        HashMap<String, Object> map = new HashMap<>();
        map.put("data", page.getResult());
        map.put("total", page.getTotal());
        return map;

    }

    @Override
    public HouseInfoVo findHouseInfo(String houseId) {
        // 获取房屋信息
        House house = houseMapper.selectById(houseId);
        // 创建房屋详情对象
        HouseInfoVo houseInfoVo = new HouseInfoVo();
        // 将查出的房屋信息copy到房屋详情对象中
        //  houseInfoVo.setHouseId(house.getHouseId());
        BeanUtils.copyProperties(house, houseInfoVo);
        // 将查出的设备信息解析成一个数组
        String[] equipments = house.getEquipments().split(",");
        // 将解析出的设备的数组存入房屋详情中
        houseInfoVo.setEquipments(equipments);
        // 根据房屋的id查询出对应的图片
        List<String> images = houseMapper.selectImageByHouseId(house.getHouseId());
        houseInfoVo.setImages(images);
        // 返回房屋详情
        return houseInfoVo;
    }

    @Override
    public void changeHouseState(HouseChangeStateDto dto) throws IOException {
        houseMapper.changeHouseState(dto);
        DeleteRequest request = new DeleteRequest("house", dto.getHouseId());
        restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        if (dto.getStates() == 1) {
            House house = houseMapper.selectById(dto.getHouseId());
            HouseDoc houseDoc = new HouseDoc(house);
            IndexRequest indexRequest = new IndexRequest("house").id(houseDoc.getHouseId());
            String s = JSONUtil.toJsonStr(houseDoc);
            System.out.println(s);
            indexRequest.source(s, XContentType.JSON);
            IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            System.out.println(index.getResult());
        }
    }

    @Override
    public List<HouseMapVo> getHouseMaps() {
        List<HouseMapVo> houseMapVos = houseMapper.selectHouseMaps();
        return houseMapVos;
    }
}
